package com.jari.lg.service.analysis.device;

import com.jari.lg.domain.AreaBase;
import com.jari.lg.domain.DevBase;
import com.jari.lg.domain.DevErrReasonHis;
import com.jari.lg.mapper.DevErrReasonHisMapper;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.UUIDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DevErrReasonHisService {

    @Resource
    private DevErrReasonHisMapper devErrReasonHisMapper;

    protected void CreateDevErrReasonHis() throws ParseException {
        List<DevErrReasonHis> ans = new ArrayList<>();

        for (AreaBase a : BaseData.areaBaseList) {
            List<DevBase> devBaseList = BaseData.devBaseList.stream().filter(item -> item.getAreacode().equals(a.getAreacode())).collect(Collectors.toList());

            //每个区下面的设备生产与天气相关的数据
            if (!devBaseList.isEmpty()) {
                //System.out.println("该区域下的设备生产数据");
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(format1.parse("2021-04-01 10:05:30"));//从2022年1月开始生产数据
                //一年12个月
                for (int i = 0; i < 12; i++) {
                    //随机10个设备
                    Set<Integer> Index = new HashSet<>();
                    if (devBaseList.size() > 10) {
                        do {
                            Index.add(new Random().nextInt(devBaseList.size()));
                        } while (Index.size() < 10);
                    } else {
                        int ii = 0;
                        do {
                            Index.add(ii);
                            ii += 1;
                        } while (ii < devBaseList.size());
                    }
                    Iterator<Integer> Idx = Index.iterator();

                    while (Idx.hasNext()) {
                        int devIndex = Idx.next();
                        DevErrReasonHis devErrReasonHis = new DevErrReasonHis();
                        devErrReasonHis.setUuid(UUIDUtils.getUUID());
                        devErrReasonHis.setProvince("上海市");
                        devErrReasonHis.setCityid("001");
                        devErrReasonHis.setCityname("上海市");
                        devErrReasonHis.setDistrictid(a.getAreacode());
                        devErrReasonHis.setDistrictname(a.getAreaname());
                        devErrReasonHis.setDeviceid(devBaseList.get(devIndex).getDevicecode());
                        devErrReasonHis.setDevicename(devBaseList.get(devIndex).getDevicename());
                        devErrReasonHis.setDevicetype(devBaseList.get(devIndex).getDevicetype());
                        devErrReasonHis.setWeather(String.valueOf(new Random().nextInt(10) + 1));
                        calendar.add(Calendar.DATE, new Random().nextInt(20));//时间随机加几天
                        devErrReasonHis.setStartlosedatatime(calendar.getTime());
                        devErrReasonHis.setDuration(new Random().nextInt(200) + 1);
                        devErrReasonHis.setDatetime(calendar.getTime());
                        ans.add(devErrReasonHis);
                        //System.out.println("得到一条数据");
                        calendar.set(Calendar.DAY_OF_MONTH, 1);//时间重置为每月第一天
                    }
                    calendar.add(Calendar.MONTH, 1);//月份加1
                }
                //System.out.println("一个区域完成" + a.getAreaname());
            }


        }
        devErrReasonHisMapper.insertBatch(ans);

    }
}
